//
// Created by root on 9/11/25.
//
#include "otl_arch.h"

namespace enrigin {
        Architecture GetBaseArchitecture() {
#ifdef _WIN32
                // Windows系统通过预定义宏判断
#ifdef _M_X64
        return Architecture::x86_64;
#elif _M_IX86
        return Architecture::x86;
#elif _M_ARM64
        return Architecture::arm64;
#elif _M_ARM
        return Architecture::ARM;
#else
        return Architecture::UNKNOWN;
#endif

#elif __linux__
                // Linux系统通过预定义宏判断
#ifdef __x86_64__
                return Architecture::x86_64;
#elif __i386__
        return Architecture::x86;
#elif __aarch64__
        return Architecture::arm64;
#elif __arm__
        return Architecture::arm;
#elif __powerpc__
        return Architecture::powerpc;
#elif __mips__
        Architecture::mips;
#else
        return Architecture::UNKNOWN;
#endif

#elif __APPLE__
        // macOS系统通过sysctl判断
#include <sys/sysctl.h>
        int mib[2] = {CTL_HW, HW_MACHINE_ARCH};
        char arch[256];
        size_t len = sizeof(arch);

        if (sysctl(mib, 2, arch, &len, nullptr, 0) == 0) {
            std::string arch_str(arch);
            if (arch_str == "x86_64") return Architecture::x86_64;
            if (arch_str == "i386") return Architecture::x86;
            if (arch_str == "arm64") return Architecture::arm64;
            if (arch_str.find("arm") != std::string::npos) return Architecture::arm;
        }
        return Architecture::UNKNOWN;

#else
        return Architecture::UNKNOWN;
#endif
        }

        // 获取架构的字符串表示
        std::string GetArchitectureName(Architecture arch) {
                switch (arch) {
                        case Architecture::x86: return "x86";
                        case Architecture::x86_64: return "x86_64";
                        case Architecture::arm: return "arm";
                        case Architecture::arm64: return "arm64";
                        case Architecture::powerpc: return "powerpc";
                        case Architecture::mips: return "mips";
                        default: return "unknown";
                }
        }
}
